package p.ithorns.framework.minio.model;

import java.io.Serializable;

/***
 * Result
 * 统一返回结果
 *
 * @author 2020/9/14-14:26 by Ht.L
 */
public final class Result<T> implements Serializable {

    /**
     * 失败
     */
    public static final int FAILED = 0;

    /**
     * 成功
     */
    public static final int SUCCESS = 1;


    /**
     * 数据对象
     */
    private T data;

    /**
     * 状态
     */
    private int status;

    /**
     * 描述/错误信息
     */
    private String msg;

    public Result() {
    }

    public Result(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public boolean isSuccess() {
        return SUCCESS == status;
    }

    public static Result<String> succeed() {
        return succeedWith(SUCCESS, null, "SUCCESS");
    }

    public static Result<String> succeed(String msg) {
        return succeedWith(SUCCESS, msg, null);
    }

    public static <T> Result<T> succeed(int status, String msg) {
        return new Result<>(status, msg, null);
    }

    public static <T> Result<T> succeedWith(T content) {
        return succeedWith(SUCCESS, "", content);
    }

    public static <T> Result<T> succeedWith(String msg, T content) {
        return new Result<>(SUCCESS, msg, content);
    }

    public static <T> Result<T> succeedWith(int status, String msg, T content) {
        return new Result<>(status, msg, content);
    }

    public static Result<String> failed() {
        return failed("FAILED");
    }

    public static Result<String> failed(String msg) {
        return failed(FAILED, msg);
    }

    public static <T> Result<T> failed(int status, String msg) {
        return failedWith(status, msg, null);
    }

    public static <T> Result<T> failedWith(int status, String msg, T content) {
        return new Result<>(status, msg, content);
    }

    public static <T> Result build(boolean b) {
        return b ? succeed() : failed();
    }

    /**
     * 是否成功响应
     */
    public static boolean isSuccess(int status) {
        return SUCCESS == status;
    }

}